// pages/adress/adress.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
// pages/homePage/homePage.js
var QQMapWX = require('../../qqmap-wx-jssdk.min.js'); // 引入腾讯地图SDK核心类
var that = this,
  qqmapsdk;
qqmapsdk = new QQMapWX({
  key: 'LGGBZ-JGS6P-6IHDP-L7KU6-EORV3-Y6FHH'
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wpxIos: false,
    list: [],
    moadelShow: false,
    formInfo: {
      consignee: '',
      mobile: '',
      province: '',
      city: '',
      district: '',
      address: '',
      label:0,
      is_default: 0
    },
    // 是否编辑
    Edit: false,
    EditIndex: null,
    // 是否从购买页跳转
    Pay: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      wpxIos: getApp().globalData.wpxIos,
      // list: wx.getStorageSync('adressInfo') || []
    })
    if (options.pay) {
      this.setData({
        Pay: true
      })
    }
    this.getAdress()
  },
  // 选中地址
  clickAdress(e){
    if (this.data.pay==false) {
      return
    }
    getApp().globalData.adressInfo=e.currentTarget.dataset.row
    wx.navigateBack()
  },

  getAdress(){
    JsonPost('user/address-list').then(res=>{
      this.setData({
        list: res.data || []
      })
    })
  },

  // 一键获取地址
  oneGet() {
    let that=this
    wx.chooseAddress({
      success(res) {
        console.log(res);
        let obj = {
          consignee: res.userName,
          mobile: res.telNumber,
          province: res.provinceName,
          city: res.cityName,
          district: res.countyName,
          address: res.detailInfo,
          label:0,
          is_default: 0
        }
        that.setData({
          formInfo:obj
        })
      }
    })
  },

  // 选中默认
  radioChange(e) {
    let that=this
    console.log(e.detail.value)
    JsonPost('user/set-default-address',{'id':that.data.list[e.detail.value].id}).then(res=>{
      that.getAdress()
    })
  },

  // 编辑
  edit(e) {
    this.setData({
      formInfo: this.data.list[e.currentTarget.dataset.index],
      moadelShow: true,
      Edit: true,
      EditIndex: e.currentTarget.dataset.index
    })
  },

  // 删除
  del(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          JsonPost('user/delete-address',{'id':that.data.list[e.currentTarget.dataset.index].id}).then(res=>{
            that.getAdress()
          })
        }
      }
    })
  },

  // 添加/编辑收货地址
  add() {
    this.setData({
      formInfo: {
        consignee: '',
        mobile: '',
        province: '',
        city: '',
        district: '',
        address: '',
        label:0,
        is_default: 0
      },
      moadelShow: true,
      Edit: false,
      EditIndex: null
    })
  },
  // 关闭
  closMoadel() {
    this.setData({
      moadelShow: false
    })
  },

  // 输入姓名
  bindName(e) {
    let str = "formInfo.consignee"
    this.setData({
      [str]: e.detail.value
    })
  },

  // 输入联系电话
  bindPhone(e) {
    if (!(/^1[3456789]\d{9}$/.test(e.detail.value.trim()))) {
      wx.showToast({
        title: '手机号码有误，请重填',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let str = "formInfo.mobile"
    this.setData({
      [str]: e.detail.value.trim()
    })
  },

  // 输入详细地址
  bindAdress(e) {
    let str = "formInfo.address"
    this.setData({
      [str]: e.detail.value
    })
  },

  // 选择地区
  pickLocation(e) {
    console.log(e, 'pickLocation')
    let str = 'formInfo.province'
    let str1 = 'formInfo.city'
    let str2 = 'formInfo.district'
    // let str1 = 'formInfo.code'
    this.setData({
      [str]: e.detail.value[0],
      [str1]: e.detail.value[1],
      [str2]: e.detail.value[2],
    })
  },

  // 获取地理位置
  getDinwei() {
    wx.showLoading({
      title: '正在获取定位',
      mask: true
    })
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log('latitude:', latitude, 'longitude:', longitude);

        //根据经纬度获取所在城市
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            let str = 'formInfo.address'
            that.setData({
              [str]: res.result.formatted_addresses.recommend
            })
            wx.hideLoading()
          }
        });
      }
    })
  },

  // 保存
  submit() {
    for (let key in this.data.formInfo) {
      if (this.data.formInfo[key] == '' && this.data.formInfo[key].length < 1) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    JsonPost('user/operate-address',this.data.formInfo).then(res=>{
      this.getAdress()
      this.setData({
        // list: arr,
        moadelShow: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})